package cn.iocoder.yudao.module.member.controller.admin.siteconfig;

import cn.hutool.core.text.CharSequenceUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.member.controller.admin.config.vo.MemberConfigRespVO;
import cn.iocoder.yudao.module.member.controller.admin.config.vo.MemberConfigSaveReqVO;
import cn.iocoder.yudao.module.member.controller.admin.siteconfig.vo.SiteConfigurationVo;
import cn.iocoder.yudao.module.member.convert.config.MemberConfigConvert;
import cn.iocoder.yudao.module.member.convert.siteconfiguration.SiteConfigurationConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.config.MemberConfigDO;
import cn.iocoder.yudao.module.member.dal.dataobject.siteconfiguration.SiteConfigurationDO;
import cn.iocoder.yudao.module.member.service.config.MemberConfigService;
import cn.iocoder.yudao.module.member.service.siteconfiguration.SiteConfigurationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jodd.util.StringUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 简易代销
 */
@Tag(name = "管理后台 - 前端站点配置")
@RestController
@RequestMapping("/member/site/configuration")
@Validated
public class SiteConfigurationController {

    @Resource
    private FileApi fileApi;


    @Resource
    private SiteConfigurationService siteConfigurationService;

    @PostMapping("/save")
    @Operation(summary = "保存前端站点配置")
    @PreAuthorize("@ss.hasPermission('member:site:configuration:save')")
    public CommonResult<Boolean> saveConfig(@Valid @RequestBody SiteConfigurationVo saveReqVO) {
        siteConfigurationService.saveConfig(saveReqVO);
        if(StringUtil.isNotBlank(saveReqVO.getLogUrl())){
            fileApi.moveFile( CharSequenceUtil.subAfter( saveReqVO.getLogUrl(), "/get/", false),"icon");
        }
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得前端站点配置")
//    @PreAuthorize("@ss.hasPermission('member:site:configuration:query')")
    public CommonResult<SiteConfigurationVo> getConfig() {
        SiteConfigurationVo config = siteConfigurationService.getConfig();
        return success(config);
    }


    @PostMapping("/update")
    @Operation(summary = "修改前端站点配置")
    @PreAuthorize("@ss.hasPermission('member:site:configuration:update')")
    public CommonResult<SiteConfigurationVo> updateConfig( @RequestBody  SiteConfigurationVo vo) {
        SiteConfigurationVo config = siteConfigurationService.updateConfig(vo);
        if(StringUtil.isNotBlank(vo.getLogUrl())){
            fileApi.moveFile( CharSequenceUtil.subAfter( vo.getLogUrl(), "/get/", false),"icon");
        }
        return success(config);
    }
}
